#pragma once

#include "task.h"
#include <atomic>
#include <map>
#include <mutex>

namespace OHOS {
namespace SmartServe {

using TaskID = uint64_t;

class TaskCenter {
public:
    // 添加任务，返回系统生成的全局唯一 id
    TaskID Add(std::shared_ptr<EvalTask> task)
    {
        TaskID id = nextId_++; // 原子递增
        std::unique_lock<std::mutex> lock(mutex_);
        tasks_.emplace(id, std::move(task));
        return id;
    }

    std::shared_ptr<EvalTask> Get(TaskID id)
    {
        std::unique_lock<std::mutex> lock(mutex_);
        auto it = tasks_.find(id);
        return it == tasks_.end() ? nullptr : it->second;
    }

    bool Remove(TaskID id)
    {
        std::unique_lock<std::mutex> lock(mutex_);
        return tasks_.erase(id) > 0;
    }

private:
    std::map<TaskID, std::shared_ptr<EvalTask>> tasks_; // 有序容器
    std::atomic<TaskID> nextId_{1};
    std::mutex mutex_;
};

} // namespace SmartServe
} // namespace OHOS
